package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.pubdef.dto.guild.GuildCfgAdminDTO;
import com.qumitech.visualnovel.service.guild.GuildService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @className: GuildController
 * @description:
 * @author: bzhao
 * @date: 2020/10/12 8:36 下午
 * copyright(c) 2017-2020 趣米（武汉）科技有限公司
 */
@RestController
@RequestMapping("/api/guild")
public class GuildController {

    @Autowired
    GuildService guildService;


    @GetMapping("")
    public BaseRsp listGuildConfig(@RequestParam(value = "limit", required = false, defaultValue = "20") String limit,
                                   @RequestParam(value = "offset", required = false, defaultValue = "0") String offset,
                                   @RequestParam(value = "owner_uid", required = false) Long ownerUid,
                                   @RequestParam(value = "guild_id", required = false) Integer guildId,
                                   @RequestParam(value = "name", required = false) String name,
                                   @RequestParam(value = "delete_flag", required = false) Byte deleteFlag,
                                   @RequestParam(value = "start_time", required = false) String startTime,
                                   @RequestParam(value = "end_time", required = false) String endTime) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("limit", limit);
        paramMap.put("offset", offset);
        if (ownerUid != null) {
            paramMap.put("owner_uid", String.valueOf(ownerUid));
        }
        if(guildId != null){
            paramMap.put("guild_id", String.valueOf(guildId));
        }
        if(StringUtils.isNotEmpty(name)){
            paramMap.put("name", name);
        }
        if(deleteFlag != null){
            paramMap.put("delete_flag", String.valueOf(deleteFlag));
        }
        if(startTime != null){
            paramMap.put("start_time", startTime);
        }
        if(endTime != null){
            paramMap.put("end_time", endTime);
        }
        return guildService.listGuildCfg(paramMap);
    }

    @PostMapping("")
    public BaseRsp createGuild(@RequestBody GuildCfgAdminDTO dto) {
        guildService.createGuild(dto);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PostMapping("/{guild_id}")
    public BaseRsp editActivityContentConfig(@PathVariable(name = "guild_id") Integer guildId, @RequestBody GuildCfgAdminDTO dto) {
        guildService.updateGuildCfg(guildId,dto);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping("/{guild_id}")
    public BaseRsp deleteActivityContentConfig(@PathVariable(name = "guild_id") Integer guildId) {
        guildService.deleteGuild(guildId);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @GetMapping("/member/list")
    public BaseRsp listMembers(@RequestParam(value = "member_uid", required = false) Integer memberUid,
                               @RequestParam(value = "guild_id", required = false) Integer guildId,
                               @RequestParam(value = "status", required = false) Integer status,
                               @RequestParam(value = "start_time", required = false) String startTime,
                               @RequestParam(value = "end_time", required = false) String endTime,
                               @RequestParam(value = "offset", required = false, defaultValue = "0") int offset,
                               @RequestParam(value = "limit", required = false, defaultValue = "20") int limit) {
        return guildService.listMembers(memberUid, guildId, status, startTime, endTime, offset, limit);
    }

    @GetMapping("/member/options/guilds")
    public BaseRsp listMemberOptionsGuild() {
        return guildService.listMemberOptionsGuild();
    }

    @GetMapping("/member/options/status")
    public BaseRsp listMemberOptionsStatus() {
        return guildService.listMemberOptionsStatus();
    }
}
